/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.service.export.xls;

import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import org.tnmk.worktime.gui.SpendAnalysisController;
import org.tnmk.worktime.service.SessionService;
import org.tnmk.worktime.service.dao.entry.SessionEntry;
import org.tnmk.worktime.service.dao.entry.SpendEntry;
import org.tnmk.worktime.service.export.xls.format.XlsFormatFactory;

/**
 *
 * @author tnmkhoi
 */
public class SpendAnalysisExport extends ExcelExporter<JTable> {

  private final SpendAnalysisController controller;
  private static final int ROW_DATA_HEADER = 1;
  private static final int ROW_DATA_INIT = 2;
  private static final int COL_SESSION_INTIME = 0;
  private static final int COL_SESSION_INTIME_WIDTH = 22;
  private static final int COL_NOTE = 1;
  private static final int COL_NOTE_WIDTH = 75;
  private static final int COL_COST = 2;
  private static final int COL_COST_WIDTH = 15;
  private static final int COL_SPEND_ID = 3;

  public SpendAnalysisExport(SpendAnalysisController controller) {
    this.controller = controller;
  }

  public void export() {
    try {
      super.export(this.controller.getFrame().getJtblSpendAnalysis());
      controller.getFrame().setStatus("Export: " + ExcelExporter.EXPORT_FILE.getAbsolutePath() + "");
    } catch (WriteException ex) {
      Logger.getLogger(SpendAnalysisExport.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  @Override
  protected void export(WritableSheet s, JTable data) throws WriteException {
    s.setName("Spend Analysis");

    //Header
    exportDataHeader(s, "Session", "Note", "Cost", "Id");

    //Export Body
    int nextRow = exportSpend(s, data);

    //Export Footer
    exportDataFooter(s, nextRow);
  }

  protected void exportDataHeader(WritableSheet s, String... headers) throws WriteException {
    WritableCellFormat cellFormat = XlsFormatFactory.newBoldFormat();
    for (int col = 0; col < headers.length; col++) {
      String header = headers[col];
      s.addCell(new Label(col, ROW_DATA_HEADER, header, cellFormat));
    }
  }

  protected int exportSpend(WritableSheet s, JTable jtbl) throws WriteException {
    int exportRow = ROW_DATA_INIT;
    s.setColumnView(COL_SESSION_INTIME, COL_SESSION_INTIME_WIDTH);
    s.setColumnView(COL_NOTE, COL_NOTE_WIDTH);
    s.setColumnView(COL_COST, COL_COST_WIDTH);

    TableModel model = jtbl.getModel();
    for (int i = 0; i < model.getRowCount(); i++) {
      SpendEntry spend = controller.toFilteredSpendEntry(i);
      Map<String, Object> extVal = (Map) jtbl.getValueAt(i, SpendAnalysisController.COL_EXT);
      Boolean selected = (Boolean) extVal.get(SpendAnalysisController.EXTKEY_ROW_SELECTED);
      if (selected) {
        exportSpend(s, spend, exportRow);
        exportRow++;
      }
    }
    return exportRow;
  }

  protected void exportSpend(WritableSheet s, SpendEntry spend, int row) throws WriteException {
    WritableCellFormat numCellFormat = XlsFormatFactory.newIntegerFormat();
    WritableCellFormat dateCellFormat = XlsFormatFactory.newDateTimeFormat();

    SessionService service = controller.getFrame().getService();
    SessionEntry session = service.findSession(spend.getSessionId());
    Date inTime = session.getInTime().toDate();
    s.addCell(new jxl.write.Number(COL_COST, row, spend.getCost(), numCellFormat));
    s.addCell(new jxl.write.Number(COL_SPEND_ID, row, spend.getId(), numCellFormat));
    s.addCell(new Label(COL_NOTE, row, spend.getNote()));
    s.addCell(new DateTime(COL_SESSION_INTIME, row, inTime, dateCellFormat));
  }

  private void exportDataFooter(WritableSheet s, int row) throws WriteException {
    WritableCellFormat numCellFormat = XlsFormatFactory.newIntegerFormat();
    
    String strFormula = "SUM(C"+ROW_DATA_INIT+":C"+(row)+")";
    Formula formula = new Formula(COL_COST, row, strFormula, numCellFormat);
    s.addCell(formula);
  }
}
